<?php
/**
 * Created by PhpStorm.
 * User: 王峥宇
 * Date: 2019-04-23
 * Time: 14:38
 */

namespace app\admin\controller;

use think\Controller;
use think\Session;

class Index extends Base
{
    public function index()
    {
        //获取用户id
        $admin_uid = Session::get("admin_uid");
        //查出用户组rules
        $admin_info = db("auth_group g")
            ->join("jc_auth_group_access ga","g.id = ga.group_id")
            ->where("ga.uid",$admin_uid)
            ->field("g.rules")
            ->find();
        $where ['id'] = array("in",$admin_info['rules']);
        $where ['pid'] = 0;
        $whereDate['id']= array("in",$admin_info['rules']);
        $res=db('auth_rule')->where($where)->select();
        foreach($res as $k=>$v){
            $res[$k]['son']=db('auth_rule')->where($whereDate)->where('pid',$v['id'])->select();
        }
        return view('index',['res'=>$res]);
    }

   public function welcome()
    {
        $time = date('Y-m-d');
        $uid=Session::get('admin_uid');
        $gid=db('guanli')->where('id',$uid)->find();
        $arr=db('user')->where('uid',$gid['uid'])->find();
        if($arr['daili']==1)
        {
            //总代理数
            $zvip=db('user')->where('u_path','like',',%'.$gid['uid'].',%')->count();
            //当日新增代理
            $xvip = db('user')->where('u_path','like',',%'.$gid['uid'].',%')->whereTime('u_createtime',$time)->count();
            $arr = db('user')->where( 'u_path','like',',%'.$gid['uid'].',%')->select();
            foreach($arr as $v)
            {
                $where['uid']=$v['uid'];
                //代理总提现额度
                $tmoney = db('withdrawal')->where($where)->where('w_state',2)->field('w_money')->select();
                if($tmoney!=[]){
                foreach ($tmoney as $va)
                {
                    $b[]=$va['w_money'];
                }}else{
                    $b=[];
                }
                //代理已完成订单数
                $ydin = db('indent')->where($where)->where('sid',4)->select();
                if($ydin!=[]){
                    foreach ($ydin as $value)
                    {
                        $e[]=$value['t_money'];
                    }
                }else{
                    $e[]=[];
                }
                //代理总余额
                $zyue = db('user')->where($where)->select();
                if($zyue!=[]){
                foreach ($zyue as $val)
                {
                    $d[]=$val['y_money'];
                }}else{
                    $d=[];
                }
            }
            $tmoney=array_sum($b);
            $ydin=array_sum($ydin);
            $ymoney=array_sum($e);
            $zyue=array_sum($d);
            return view('welcome',
                ['zvip'=>$zvip,'gid'=>$gid,
                 'xvip'=>$xvip,
                 'tmoney'=>$tmoney,'ydin'=>$ydin,
                 'ymoney'=>$ymoney,'zyue'=>$zyue
                ]);
        }else{
            $xvip = db('user')->whereTime('u_createtime',$time)->count();
            $zvip = db('user')->count();
            $tmoney = db('withdrawal')->where('w_state',2)->sum('w_money');
          	$zyong = db('indent')->where('sid',3)->sum('t_usermoney');
          	$zyue=db('user')->sum('y_money');
            //完成订单量
            $ydin = db('indent')->where('sid',4)->count();
            //完成订单金额
            $ymoney = db('indent')->where('sid',4)->sum('t_money');
            //匹配成功订单数
            $ysum = db('indent')->where('sid',1)->count();
            //匹配成功订单金额
            $pmoney = db('indent')->where('sid',2)->sum('t_money');
            //未匹配订单数
            $nsum = db('indent')->where('sid',0)->count();
            //未匹配订单金额
            $nmoney = db('indent')->where('sid',0)->sum('t_money');
            if($tmoney=='') {
                $tmoney='0';
            }
            if($pmoney=='') {
                $pmoney='0';
            }
            if($nmoney=='') {
                $nmoney='0';
            }
            return view('welcome',
                [
                    'xvip'=>$xvip,'zvip'=>$zvip,
                    'tmoney'=>$tmoney,'ydin'=>$ydin,
                  	'zyue'=>$zyue,'zyong'=>$zyong,
                    'ymoney'=>$ymoney,'ysum'=>$ysum,
                    'pmoney'=>$pmoney,'nsum'=>$nsum,
                    'nmoney'=>$nmoney,'gid'=>$gid
                ]);
        }
    }
}